/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
 * Web application lifecycle listener.
 * @author user
 */
public class database_listener implements ServletContextListener {

    public static String CONNECTION_KEY="your_connection_key";

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            System.out.println("hai");
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/maninrelaychat", "root", "");
            sce.getServletContext().setAttribute(CONNECTION_KEY, connection);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            Connection connection = (Connection) sce.getServletContext().getAttribute(CONNECTION_KEY);
            connection.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
